extends BaseStage

const Staeg2_4Music := preload("res://assets/music/Stage2_4.mp3")

@export var push_speed:float

@onready var push_brick: StaticBody2D = $PushBrick
@onready var time_stop_area: Area2D = $TimeStopArea
@onready var spike: Area2D = $SpikesParent/Spike
@onready var spike_2: Area2D = $SpikesParent/Spike2

func _ready() -> void:
	fake_bg.visible = true
	Global.player.global_position = spawn_point.global_position
	current_npc.change_emotion(start_emotion)
	DialogHandler.dialogue_end.connect(on_dialogue_end.unbind(1))
	GameEvents.stage_entered.emit()
	SaveManager.save_game()
	spike.process_mode = Node.PROCESS_MODE_INHERIT
	spike_2.process_mode = Node.PROCESS_MODE_INHERIT
	if MusicPlayer.bgm_player.stream != Staeg2_4Music:
		MusicPlayer.play_bgm(stage_bgm)
	if Global.player.global_position == spawn_point.global_position:
		Global.player.is_dead = false

func on_dialogue_end() -> void:
	super.on_dialogue_end()
	if current_count == 1:
		var tween = create_tween()
		tween.tween_property(push_brick, "global_position:x", 2040, 2.0).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
		Global.player.can_pre_build = false
		Global.player.freeze()
		tween.tween_callback(func():
			Global.player.velocity = Vector2.RIGHT * push_speed
			current_npc.change_emotion(Npc.Emotion.MANIA)
			time_stop_area.body_entered.connect(on_timestop_body_entered)
			)
	elif current_count == 2:
		Global.player.set_paused(false)
		Global.player.no_gravity = false
		Global.player.can_pre_build = true
		Global.player.on_pre_build_pressed()

func on_timestop_body_entered(body:Node2D) -> void:
	Global.player.freeze()
	Global.player.no_gravity = true
	current_npc.action()
	time_stop_area.queue_free()


func _on_check_point_3_body_entered(body: Node2D) -> void:
	MusicPlayer.play_bgm(Staeg2_4Music)
